//
//  LTOVEDynamicCreakButton.m
//  dsad
//
//  Created by LTOVE on 16/8/4.
//  Copyright © 2016年 LTOVE. All rights reserved.
//

#import "LTOVEDynamicCreakButton.h"
#import "UIImage+LTOVEImageWithColor.h"
#define MaginTop 10

@interface LTOVEDynamicCreakButton (){
    
    UIView *_vvvvv;
    NSDictionary *_dict;
}

@property (nonatomic,strong)NSMutableArray *btnArray;

@end

@implementation LTOVEDynamicCreakButton


+ (LTOVEDynamicCreakButton *)buttonViewWithFrame:(CGRect)frame titleDataArray:(NSArray *)titleArray andEdgInset:(UIEdgeInsets)inset andMaginX:(CGFloat)maginX andMaginY:(CGFloat)maginY andBtnHeigh:(CGFloat)btnHeigh andBtnNorColor:(UIColor *)color andBtnHeighColor:(UIColor *)heighcolor andRadius:(CGFloat)radius andFont:(NSDictionary *)font
{
    return [LTOVEDynamicCreakButton buttonViewWithFrame:frame titleDataArray:titleArray andCol:(int)titleArray.count andEdgInset:inset andMaginX:maginX andMaginY:maginY andBtnHeigh:btnHeigh andBtnNorColor:color andBtnHeighColor:heighcolor andRadius:radius andFont:font];
}

+ (LTOVEDynamicCreakButton *)buttonViewWithFrame:(CGRect)frame titleDataArray:(NSArray *)titleArray andCol:(int)col andEdgInset:(UIEdgeInsets)inset andMaginX:(CGFloat)maginX andMaginY:(CGFloat)maginY andBtnHeigh:(CGFloat)btnHeigh andBtnNorColor:(UIColor *)color andBtnHeighColor:(UIColor *)heighcolor andRadius:(CGFloat)radius andFont:(NSDictionary *)font
{
    LTOVEDynamicCreakButton *view = [[LTOVEDynamicCreakButton alloc]initWithFrame:frame];
    view.backgroundColor = [UIColor whiteColor];//设置视图背景颜色
//    使用九宫格创建button
    [view setUpButtonWithView:view andDataArray:titleArray andCol:col andEdgInset:inset andMaginX:maginX andMaginY:maginY andBtnHeigh:btnHeigh andBtnNorColor:color andBtnHeighColor:heighcolor andRadius:radius andFont:font];
    
    return view;
}

- (void)setUpButtonWithView:(UIView *)view andDataArray:(NSArray *)dataArray andCol:(int)col andEdgInset:(UIEdgeInsets)inset andMaginX:(CGFloat)maginX andMaginY:(CGFloat)maginY andBtnHeigh:(CGFloat)btnHeigh andBtnNorColor:(UIColor *)color andBtnHeighColor:(UIColor *)heighcolor andRadius:(CGFloat)radius andFont:(NSDictionary *)font
{
    _dict = font;
    CGFloat viewWidth = view.frame.size.width;
    CGFloat viewHeigh = view.frame.size.height;
    UIView *btnView = [[UIView alloc]initWithFrame:CGRectMake(inset.left, inset.top, viewWidth - inset.left - inset.right, viewHeigh - inset.top - inset.bottom)];
    _vvvvv = btnView;
    btnView.backgroundColor = view.backgroundColor;
    [view addSubview:btnView];
    btnView.backgroundColor = [UIColor clearColor];
//    CGFloat width = (viewWidth - inset.left - inset.righ;t);
    //创建button加到btnview 上
    CGFloat width = (btnView.frame.size.width - (col - 1) * maginX) / col;
    for (int i = 0; i < dataArray.count; i++) {
        
        CGFloat row = i / col;
        CGFloat cols = i % col;
        CGFloat x = (maginX + width) * cols;
        CGFloat y = (maginY + btnHeigh) * row;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(x, y, width, btnHeigh);
//        btn.backgroundColor = color;
        NSString *str = dataArray[i];
        [btn setTitle:str forState:UIControlStateNormal];
        btn.layer.cornerRadius = radius;
        btn.layer.masksToBounds = YES;
        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]initWithString:str attributes:font];
        [btn setAttributedTitle:attrString forState:UIControlStateHighlighted];
        [btn setAttributedTitle:attrString forState:UIControlStateNormal];
//        [btn setImage:[UIImage imageWithColor:color size:btn.frame.size] forState:UIControlStateNormal];
//        [btn setImage:[UIImage imageWithColor:heighcolor size:btn.frame.size] forState:UIControlStateHighlighted];
        
        [btn setBackgroundImage:[UIImage imageWithColor:color size:btn.frame.size] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageWithColor:heighcolor size:btn.frame.size] forState:UIControlStateHighlighted];
//        [btn setBackgroundColor:color];
        btn.tag = i;
        if (!_btnArray) {
            _btnArray = [NSMutableArray array];
        }
        [_btnArray addObject:btn];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btnView addSubview:btn];
    }
    
}

+ (LTOVEDynamicCreakButton *)buttonViewWithFrame:(CGRect)frame titleDataArray:(NSArray *)titleArray andCol:(int)col andEdgInset:(UIEdgeInsets)inset andMaginX:(CGFloat)maginX andMaginY:(CGFloat)maginY andBtnHeigh:(CGFloat)btnHeigh andBtnNorImage:(UIImage *)norImage andHeighImage:(UIImage *)heighImage andRadius:(CGFloat)radius andFont:(NSDictionary *)font
{
    return [LTOVEDynamicCreakButton buttonViewWithFrame:frame titleDataArray:titleArray andCol:col andEdgInset:inset andMaginX:maginX andMaginY:maginY andBtnHeigh:btnHeigh andBtnNorColor:[UIColor colorWithPatternImage:norImage] andBtnHeighColor:[UIColor colorWithPatternImage:heighImage] andRadius:radius andFont:font];
}

+ (LTOVEDynamicCreakButton *)buttonViewWithFrame:(CGRect)frame titleDataArray:(NSArray *)titleArray andEdgInset:(UIEdgeInsets)inset andMaginX:(CGFloat)maginX andMaginY:(CGFloat)maginY andBtnHeigh:(CGFloat)btnHeigh andBtnNorImage:(UIImage *)norImage andHeighImage:(UIImage *)heighImage andRadius:(CGFloat)radius andFont:(NSDictionary *)font
{
    return [LTOVEDynamicCreakButton buttonViewWithFrame:frame titleDataArray:titleArray andCol:(int)titleArray.count andEdgInset:inset andMaginX:maginX andMaginY:maginY andBtnHeigh:btnHeigh andBtnNorColor:[UIColor colorWithPatternImage:norImage] andBtnHeighColor:[UIColor colorWithPatternImage:heighImage] andRadius:radius andFont:font];
}


- (void)btnClick:(UIButton *)btn
{
//    NSLog(@"%@",_btnArray);
    [btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    if ([_delegate respondsToSelector:@selector(dynamicButtonClick:)]) {
        [_delegate dynamicButtonClick:btn];
    }
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state index:(NSUInteger)index
{
    if (index > _btnArray.count - 1) {
        return;
    }
    UIButton *btn = _btnArray[0];
//    for (UIButton *btn in _vvvvv.subviews) {
//        if (btn.tag == index) {
//            [btn setTitle:title forState:state];
//        }
//    }
    
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]initWithString:title attributes:_dict];
    [btn setAttributedTitle:attrString forState:UIControlStateHighlighted];
    [btn setAttributedTitle:attrString forState:UIControlStateNormal];
//    [btn setTitle:@"ssss" forState:UIControlStateNormal];
//    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
//    [btn setBackgroundImage:[UIImage imageWithColor:[UIColor greenColor] size:btn.size] forState:UIControlStateNormal];
//    [btn removeFromSuperview];
    
}

@end
